package stdlib


import "regexp"
import "strings"


// tag = %[flags][width][.precision]specifier
var tagPattern = regexp.MustCompile(`%[ #=-]?[0-9]*(\.[0-9]+)?[cdeEfGgioqsuxX%]`)

func parseFmtStr(fmt string) []string {
	if fmt == "" || strings.IndexByte(fmt, '%') < 0 {
		return []string{fmt}
	}

	parsed := make([]string, 0,  len(fmt)/2)
	for {
		if fmt == "" {
			break
		}

		loc := tagPattern.FindStringIndex(fmt)
		if loc == nil {
			parsed = append(parsed, fmt)
			break
		}

		head := fmt[:loc[0]]
		tag := fmt[loc[0]:loc[1]]
		tail := fmt[loc[1]:]

		if head != "" {
			parsed = append(parsed, head)
		}
		parsed = append(parsed, tag)
		fmt = tail
	}

	return parsed
}


func find(s, pattern string, init int, plain bool) (start, end int) {
	tail := s
	if init > 1 {
		tail = s[init-1:]
	}

	if plain {
		start = strings.Index(tail, pattern)
		end = start + len(pattern) - 1
	} else {
		re, err := _compile(pattern)
		if err != "" {
			panic(err) // todo
		} else {
			loc := re.FindStringIndex(tail)
			if loc == nil {
				start, end = -1, -1
			} else {
				start, end = loc[0], loc[1]-1
			}
		}
	}

	if start >= 0 {
		start += len(s) - len(tail) + 1
		end += len(s) - len(tail) + 1
	}

	return
}


func _compile(pattern string) (*regexp.Regexp, string) {
	re, err := regexp.Compile(pattern)
	if err != nil {
		return nil, err.Error()	// TODO
	} else {
		return re, ""
	}
} 